using UnityEngine;
using System.Collections;

public class Eyes : MonoBehaviour {

	private TextMesh thisText;
	
	private float min;
	private float max;
	
	private float firstX;
	private float firstY;
	void Start()
	{
		thisText = gameObject.GetComponent<TextMesh>();
		min = transform.localScale.y/2;
		max = transform.localScale.y;
		firstX = transform.localPosition.x;
		firstY = transform.localPosition.y;
		StartCoroutine(flash());
	}
	
	void Update()
    {
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_WEBPLAYER
        float x = (0.03f*Input.GetAxis("Horizontal"))/1;
		float newPositionX = firstX + x;
		
		float y = (0.03f*Input.GetAxis("Vertical"))/1;
		float newPositionY = firstY + y;
		#endif
		
		#if UNITY_ANDROID
	    Vector3 dir = Vector3.zero;
        dir.y = Input.acceleration.y;
        dir.x = Input.acceleration.x;
        
        if (dir.sqrMagnitude > 1)
            dir.Normalize();
		
		float x = (0.03f*dir.x)/1;
		float newPositionX = firstX + x;
		
		float y = (0.03f*dir.y)/1;
		float newPositionY = firstY + y;
		
		#endif
		
		transform.localPosition	= new Vector3(newPositionX,newPositionY,transform.localPosition.z);
	}
	
	IEnumerator flash()
	{
		transform.localScale = new Vector3(transform.localScale.x,max,transform.localScale.z);
		yield return new WaitForSeconds(2f);
		transform.localScale = new Vector3(transform.localScale.x,min,transform.localScale.z);
		yield return new WaitForSeconds(0.05f);
	
		StartCoroutine(flash());
	}
	
	void OnResume()
	{		
		StartCoroutine(flash());
	}
}
